﻿using SQLite;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Application.Dao
{
    public class AppDao
    {
        private string path = @".\db\app.db";
        SQLiteConnection db = null;

        public AppDao()
        {

            FileInfo info = new FileInfo(path);
            if (!info.Directory.Exists)
            {
                info.Directory.Create();
            }

            try
            {
                db = Connect();
                db.CreateTable<CApplication>();
            }
            finally
            {
                DisConnect();
            }
        }

        public void Insert(CApplication t)
        {
            try
            {
                db = Connect();
                db.Insert(t);
            }
            finally
            {
                db.Close();
            }
        }

        public List<CApplication> Select()
        {
            try
            {
                db = Connect();
                return db.Table<CApplication>().ToList();
            }
            finally
            {
                db.Close();
            }
        }

        public void Delete(CApplication t)
        {
            try
            {
                db = Connect();
                db.Delete(t);
            }
            finally
            {
                db.Close();
            }
        }

        public void Update(CApplication t)
        {
            try
            {
                db = Connect();
                db.Update(t);
            }
            finally
            {
                db.Close();
            }
        }

        protected SQLiteConnection Connect()
        {
            return new SQLiteConnection(path);
        }

        protected void DisConnect()
        {
            db.Close();
        }
    }
}